//
// Created by lenovo on 2024/1/13.
//
#include <utility>

#include "export.h"
#include "./lexer/Lexer.h"
#include "./Slicer/Slicer.h"
#include "./Tokenize/Tokenizer.h"
#include <iostream>
using namespace std ;


Evaluable* Translator::result() {
    if (not _done)
        this->translate();
    _done = true ;
    return this->_result;
}

void Translator::translate() {
    try{
        //翻译流程
        Slicer sc(this->material);
        this->words = sc.result();

        Tokenizer tk(this->words);
        this->tokens = tk.result();

        Lexer lx(tk.result() , this->_identified_funcs , this->priority);
        this->_result = lx.root();

    }catch (const exception& e){
        std::string _exp = "\nERROR in the process when translating strings into tokens then into Evaluable units:\n";
        _exp += "The concrete Error is :\n\t";
        _exp += e.what();
        throw std::logic_error(_exp);
    }
}


void showToken(token* t){
    if (t->type == token::FUNCTION){
        cout << ((Funcs*)t->base)->FuncType ;
    }
    else if (t->type == token::DOT){
        cout << ',' ;
    }
    else if (t->type == token::LBRACKET){
        cout << "(" ;
    }
    else if (t->type == token::RBRACKET)
        cout << ")";
    else if (t->type == token::OPERATOR)
        cout << ((Operator*)t->base)->OptType ;
    else if (t->type == token::NUMBER)
        cout << t->base->getValue() ;
}
